package com.lipiao.food.foodFragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lipiao.food.FoodBean;
import com.lipiao.food.FoodRecycleViewAdapter;
import com.lipiao.food.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.supercharge.shimmerlayout.ShimmerLayout;

public class HotS extends Fragment {

    View rootView;
    static RecyclerView mRecyclerView;
    static FoodRecycleViewAdapter foodRecycleViewAdapter;
    static List<FoodBean> mList = new ArrayList<>();
    static String url3 = "https://home.meishichina.com/show-top-type-recipe-order-pop.html";

    //加载
    ShimmerLayout shimmerLayout ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.foodfragment_details, container, false);
        initView();
        initListener();
        return rootView;
    }

    private void initView() {
        //加载
        shimmerLayout = rootView.findViewById(R.id.shimmer_layout);
        shimmerLayout.startShimmerAnimation();
        mRecyclerView = rootView.findViewById(R.id.card_list_food_god);
        //瀑布流
        StaggeredGridLayoutManager layoutManager =new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(layoutManager);
        initFoodBeanList();
    }

    void initFoodBeanList() {
        mList = new ArrayList<>();
        //含有网络加载，所以开启新线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //从一个URL加载一个Document对象。
                try {
                    Document doc = Jsoup.connect(url3).get();
                    final Elements titleAndPic = doc.select("div.pic");// “菜品”名称和它对应的图片都在<div class="pic">中
                    Elements url = doc.select("div.detail").select("a");// 所需链接在<div class="detail">中的<a>标签里面
                    final Elements burden = doc.select("p.subcontent"); // 原料在<p class="subcontent">中

                    //添加数据
                    for (int i = 0; i < 10; i++) {
                        FoodBean element = new FoodBean("" + titleAndPic.get(i).select("a").select("img").attr("data-src"),
                                "" + titleAndPic.get(i).select("a").attr("title"),
                                "" + burden.get(i).text(),
                                "" + url);
                        mList.add(element);
                    }
                    //实例化MyAdapter并传入mList对象
                    foodRecycleViewAdapter = new FoodRecycleViewAdapter(mList);

                    //回到主线程更新UI
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            //加载
                            shimmerLayout.setVisibility(View.INVISIBLE);
                            //为RecyclerView对象mRecyclerView设置adapter
                            mRecyclerView.setAdapter(foodRecycleViewAdapter);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }//catch
            }//run
        }).start();//new thread
    }//initFoodBeanList()

    private void initListener() {

    }
}
